package com.jumper.study.front.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.jumper.study.common.api.ResultCode;
import com.jumper.study.common.exception.Asserts;
import com.jumper.study.common.util.StrUtil;
import com.jumper.study.db.dao.AccountMapper;
import com.jumper.study.db.dao.StudentMapper;
import com.jumper.study.db.model.Account;
import com.jumper.study.db.model.Student;
import com.jumper.study.front.dto.req.RegisterReq;
import com.jumper.study.front.dto.req.UpdateInfoReq;
import com.jumper.study.front.dto.resp.StudentInfo;
import com.jumper.study.front.service.StudentService;

@Service("frontStudentService")
public class StudentServiceImpl implements StudentService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public StudentInfo getInfo(Long accountId) {
        Account account = accountMapper.selectById(accountId);
        if (account == null) {
            return null;
        }
        LambdaQueryWrapper<Student> query = Wrappers.lambdaQuery();
        query.eq(Student::getAccountId, accountId);
        Student student = studentMapper.selectOne(query);
        if (student == null) {
            return null;
        }

        StudentInfo info = new StudentInfo();
        info.setName(account.getName());
        info.setUsername(account.getUsername());
        info.setNickname(student.getNickname());
        info.setAvatar(account.getAvatar());
        info.setPhone(student.getPhone());
        info.setEmail(student.getEmail());
        info.setGender(student.getGender());
        info.setBirthday(student.getBirthday());
        info.setAddress(student.getAddress());
        info.setHobby(student.getHobby());

        return info;
    }

    @Override
    @Transactional
    public boolean register(RegisterReq registerInfo) {
        boolean usernameExist = accountMapper.usernameExist(registerInfo.getUsername());
        if (usernameExist) {
            Asserts.fail(ResultCode.VALIDATE_FAILED.getCode(), "用户名已存在");
        }
        Account account = new Account();
        account.setUsername(registerInfo.getUsername());
        account.setName(registerInfo.getName());
        account.setPassword(passwordEncoder.encode(registerInfo.getPassword()));
        account.setAvatar("");
        int ret = accountMapper.insert(account);
        if (ret < 1) {
            Asserts.fail(ResultCode.VALIDATE_FAILED.getCode(), "注册失败");
        }

        Student student = new Student();
        student.setNickname(account.getUsername());
        student.setAccountId(account.getId());
        student.setPhone(registerInfo.getPhone());
        student.setEmail("");
        student.setBirthday(null);
        student.setGender(0);
        student.setAddress("");
        student.setHobby("");
        ret = studentMapper.insert(student);

        return SqlHelper.retBool(ret);
    }

    @SuppressWarnings("null")
    @Override
    @Transactional
    public boolean updateInfo(UpdateInfoReq updateInfo) {
        Long accountId = updateInfo.getAccountId();
        if (accountId == null) {
            Asserts.fail("更新失败");
        }
        Account account = accountMapper.selectById(updateInfo.getAccountId());
        if (account == null) {
            Asserts.fail("无效用户");

        }
        account.setName(updateInfo.getName());
        account.setAvatar(updateInfo.getAvatar());
        accountMapper.updateById(account);

        LambdaQueryWrapper<Student> query = Wrappers.lambdaQuery();
        query.eq(Student::getAccountId, updateInfo.getAccountId());
        Student student = studentMapper.selectOne(query);
        if (student == null) {
            Asserts.fail("无效用户");
        }
        if (!StrUtil.isEmpty(updateInfo.getNickname())) {
            student.setNickname(updateInfo.getNickname());
        }
        if (!StrUtil.isEmpty(updateInfo.getPhone())) {
            student.setPhone(updateInfo.getPhone());
        }
        if (!StrUtil.isEmpty(updateInfo.getEmail())) {
            student.setEmail(updateInfo.getEmail());
        }
        if (!StrUtil.isEmpty(updateInfo.getAddress())) {
            student.setAddress(updateInfo.getAddress());
        }
        if (!StrUtil.isEmpty(updateInfo.getHobby())) {
            student.setHobby(updateInfo.getHobby());
        }
        if (null != updateInfo.getBirthday()) {
            student.setBirthday(updateInfo.getBirthday());
        }
        if (null != updateInfo.getGender()) {
            student.setGender(updateInfo.getGender());
        }
        int ret = studentMapper.updateById(student);
        return SqlHelper.retBool(ret);
    }

    @Override
    public boolean usernameExist(String username) {
        LambdaQueryWrapper<Account> query = Wrappers.lambdaQuery();
        query.eq(Account::getUsername, username);

        return accountMapper.exists(query);
    }

}
